package View;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;

public class ViewClass implements java.util.Observer {
	private TextField myTextField;
	private Button button;
	private Button button1;
	
	public ViewClass() {
//		System.out.println("View()");	
	
		//frame in constructor and not an attribute as doesn't need to be visible to whole class
		Frame frame 		= new Frame("simple MVC");
		frame.add("North", new Label("counter"));
		
		myTextField 		= new TextField();
		frame.add("Center", myTextField);
		
		//panel in constructor and not an attribute as doesn't need to be visible to whole class
		Panel panel 		= new Panel();
		button	 		= new Button("PressMe");
		button1 = new Button("Button 2");
		panel.add(button);
		panel.add(button1);
		frame.add("South", panel);		
	
		frame.addWindowListener(new CloseListener());	
		frame.setSize(200,100);
		frame.setLocation(100,100);
		frame.setVisible(true);
	
	} //View()
	
	// Called from the Model
	public void update(Observable obs, Object obj) {
	
		myTextField.setText("" + ((Integer)obj).intValue());	//obj is an Object, need to cast to an Integer
	
	} //update()
	
	public void addController(ActionListener controller){
//		System.out.println("View      : adding controller");
		button.addActionListener(controller);	//need instance of controller before can add it as a listener 
		button1.addActionListener(controller);	//need instance of controller before can add it as a listener
	} //addController()
	
	//to initialise TextField
	public void setValue(int v){
			myTextField.setText("" + v);
	} //setValue()
	
	//public void addModel(Model m){
	//	System.out.println("View      : adding model");
	//	this.model = m;
	//} //addModel()
	
	public static class CloseListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			e.getWindow().setVisible(false);
			System.exit(0);
		} //windowClosing()
	} //CloseListener
}
